#include "selectitemview.h"
#include "common.h"

namespace fireplane
{


	SelectItemView::SelectItemView():
		_all_item(NULL),
		_call(NULL),
		_tag(-1)
	{}

	SelectItemView::~SelectItemView()
	{
		if (_all_item)
		{
			delete[] _all_item;
		}

		_all_item = NULL;
	}

	void SelectItemView::init(char** fileName,
		int arrayCount,CCPoint startPos,ClickItemCall* call)
	{
		_all_items_file_name = fileName;
		_startPos = startPos;
		_count = arrayCount;
		_call = call;

		_all_item = new CCMenuItemSprite*[_count];
		_createMenuItem();
	}

	void SelectItemView::doCall()
	{
		if (_call)
		{
			_call->onClickItem(_tag);
		}
	}

	void SelectItemView::onFocusClick(CCObject* pSender)
	{
		if (isInAnimation())
		{
			return;
		}

		CCMenuItemSprite* item = (CCMenuItemSprite*)pSender;

		CCPoint currentPos = item->getPosition();
		CCPoint desPos = currentPos;
		desPos.y += 300;

		_tag = item->getTag();
		item->runAction(
			CCSequence::create(
			CCMoveTo::create(0.5f,desPos),
			CCCallFunc::create(this,callfunc_selector(SelectItemView::doCall)),
			NULL));

		item->runAction(CCFadeOut::create(0.5f));
	}

	void SelectItemView::_createMenuItem()
	{
		float scale = 0.5f;
		CCMenu* menu = CCMenu::create();
		menu->setPosition(ccp(0,0));

		CCPoint pos = _startPos;
		for (int i = 0;i < _count;i++)
		{
			_all_item[i] = Common::itemFromNormalImage(
				_all_items_file_name[i],
				_all_items_file_name[i],
				this,
				menu_selector(SelectItemView::onFocusClick),
				false
				);

			_all_item[i]->setTag(i);

			CCSprite* selectSprite = (CCSprite*)_all_item[i]->getSelectedImage();
			selectSprite->setScale(1.05f);
			_all_item[i]->setAnchorPoint(ccp(0,0));

			_all_item[i]->setPosition(pos);

			menu->addChild(_all_item[i],0);

			if (i > 0)
			{
				_all_item[i]->setScale(scale);
				pos.x += _all_item[i]->getContentSize().width * scale;
				_all_item[i]->setOpacity(128);
			}
			else
			{
				pos.x += _all_item[i]->getContentSize().width;
			}
		}

		this->addChild(menu);

		_focus_start_pos = _all_item[0]->getPosition();
		_item_width = _all_item[0]->getContentSize().width;

		_current_focus = 0;
		_onFocusChange(false);
	}

	bool SelectItemView::isInAnimation()
	{
		for (int i = 0;i < _count;i++)
		{
			if (_all_item[i]->numberOfRunningActions() != 0)
			{
				return true;
			}
		}

		return false;
	}

	void SelectItemView::_onFocusChange(bool move)
	{
		for (int i = 0;i < _count;i++)
		{
			_all_item[i]->setEnabled(false);
		}

		_all_item[_current_focus]->setEnabled(true);

		if (move)
		{
			CCPoint pos = _focus_start_pos;
			pos.x += _item_width;

			for (int i = _current_focus + 1;i < _current_focus + 1 + 6;i++)
			{
				int idx = i % _count;

				if(idx != _current_focus){
					_all_item[idx]->runAction(CCMoveTo::create(0.5f,pos));
					_all_item[idx]->runAction(CCScaleTo::create(0.5f,0.5f,0.5f));
					_all_item[idx]->setOpacity(128);
				}

				pos.x += _item_width * 0.5f;
			}
			_all_item[_current_focus]->runAction(CCMoveTo::create(0.5f,_focus_start_pos));
			_all_item[_current_focus]->runAction(CCScaleTo::create(0.5f,1.0f,1.0f));
			_all_item[_current_focus]->setOpacity(255);
		}
	}

	bool SelectItemView::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
	{
		if (isInAnimation())
		{
			return false;
		}

		_beginTouchPos = pTouch->getLocationInView();
		_beginTouchPos = CCDirector::sharedDirector()->convertToGL(_beginTouchPos);

		return true;
	}

	void SelectItemView::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
	{

	}

	void SelectItemView::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
	{
		CCPoint endPos = pTouch->getLocationInView();
		endPos = CCDirector::sharedDirector()->convertToGL(endPos);

		float dis = endPos.x - _beginTouchPos.x;
		if (fabs(dis) > 20)
		{
			bool isChange = false;
			if (dis > 0)
			{
				_current_focus --;
				isChange = true;
			}
			else
			{
				_current_focus ++;
				isChange = true;
			}

			if (isChange)
			{
				_current_focus = _current_focus + _count;
				_current_focus = _current_focus % _count;
				_onFocusChange(true);
			}
		}
	}
}